package com.ruoyi.insert.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.profession.domain.Awards;
import com.ruoyi.profession.mapper.AwardsMapper;
import com.ruoyi.profession.service.IAwardsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 奖项信息Controller
 * 
 * @author ruoyi
 * @date 2024-06-01
 */
@RestController
@RequestMapping("/insert/awards")
public class IAwardsController extends BaseController
{
    @Autowired
    private IAwardsService awardsService;

    @Autowired
    private AwardsMapper awardsMapper;

    /**
     * 查询奖项信息列表
     */
    @PreAuthorize("@ss.hasPermi('profession:awards:list')")
    @GetMapping("/list")
    public TableDataInfo list(Awards awards)
    {
        startPage();
        List<Awards> list = awardsService.selectAwardsList(awards);
        return getDataTable(list);
    }

    /**
     * 导出奖项信息列表
     */
    @PreAuthorize("@ss.hasPermi('profession:awards:export')")
    @Log(title = "奖项信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Awards awards)
    {
        List<Awards> list = awardsService.selectAwardsList(awards);
        ExcelUtil<Awards> util = new ExcelUtil<Awards>(Awards.class);
        util.exportExcel(response, list, "奖项信息数据");
    }

    /**
     * 获取奖项信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('profession:awards:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(awardsService.selectAwardsById(id));
    }

    /**
     * 新增奖项信息
     */
    @PreAuthorize("@ss.hasPermi('profession:awards:add')")
    @Log(title = "奖项信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Awards awards)
    {

        Long basicId = awards.getBasicInfoId();
        QueryWrapper<Awards> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("basic_info_id", basicId);
        if (awardsMapper.selectCount(queryWrapper)<20){
            return toAjax(awardsService.insertAwards(awards));
        }
        return AjaxResult.countGt();
    }

    /**
     * 修改奖项信息
     */
    @PreAuthorize("@ss.hasPermi('profession:awards:edit')")
    @Log(title = "奖项信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Awards awards)
    {
        return toAjax(awardsService.updateAwards(awards));
    }

    /**
     * 删除奖项信息
     */
    @PreAuthorize("@ss.hasPermi('profession:awards:remove')")
    @Log(title = "奖项信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(awardsService.deleteAwardsByIds(ids));
    }
}
